package com.stone.elec.rename.utils;

import java.io.File;
import java.nio.charset.Charset;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import com.google.common.collect.HashBasedTable;
import com.google.common.collect.Lists;
import com.stone.elec.rename.config.ReNameConfig;
import lombok.experimental.UtilityClass;

@UtilityClass
public class FileUtils {
    private static final ReNameConfig reNameConfig = ReNameConfig.reNameConfig;

    public static void fileDicValidate(String path) {
        File file = FileUtil.file(path);
        if (file.isDirectory()) {
            String fileName = file.getName();
            if (fileName.contains(reNameConfig.getOldProjectName())) {
                fileName = fileName.replace(reNameConfig.getOldProjectName(), reNameConfig.getProjectName());
                FileUtil.rename(file, fileName, false, true);
            }
        }
    }

    public Boolean fileDicNext(File file) {

        if (FileUtil.pathEndsWith(file, ".git")) {
            return false;
        }
        //if (FileUtil.pathEndsWith(file, ".gogs")) {
        //    return false;
        //}
        if (FileUtil.pathEndsWith(file, ".idea")) {
            return false;
        }
        if (FileUtil.pathEndsWith(file, "db")) {
            return false;
        }
        return true;
    }

    public static void fileDicValidate(File file) {
        if (file.isDirectory()) {

            if (!fileDicNext(file)) {
                return;
            }

            String fileName = file.getName();
            if (fileName.contains(reNameConfig.getOldProjectName())) {
                fileName = fileName.replace(reNameConfig.getOldProjectName(), reNameConfig.getProjectName());
                file = FileUtil.rename(file, fileName, false, true);
            }

            Map<String, String> c = reNameConfig.getAPackage().getReplace();
            for (Entry<String, String> vc : c.entrySet()) {//groupId
                if (fileName.contains(vc.getKey())) {
                    fileName = fileName.replace(vc.getKey(),
                        vc.getValue());
                    file = FileUtil.rename(file, fileName, false, true);
                    break;
                }
            }
        }
    }

    public void fileNameValidate(File file) {
        String fileName = file.getName();

        if (FileUtil.pathEndsWith(file, "iml")) {
            return;
        }

        if (fileName.contains(reNameConfig.getOldProjectName())) {

            fileName = fileName.replace(reNameConfig.getOldProjectName(), reNameConfig.getProjectName());

            file = FileUtil.rename(file, fileName, false, true);
        }

        Map<String, String> c = reNameConfig.getSrcFile().getReplace();
        for (Entry<String, String> vc : c.entrySet()) {//groupId
            if (fileName.contains(vc.getKey())) {
                fileName = fileName.replace(vc.getKey(),
                    vc.getValue());
                file = FileUtil.rename(file, fileName, false, true);
                break;
            }
        }

    }

    public void pomValidate(File file) {
        String fileName = file.getName();
        if (fileName.contains("pom")) {

            String tmpStart = "";
            String tmpValue = "";
            List<String> files = FileUtil.readLines(file, Charset.defaultCharset());
            List<String> newFiles = Lists.newArrayList();
            for (String v : files) {
                String content = v;
                if (content.contains("~")) {
                    continue;
                }

                if (StrUtil.isNotBlank(tmpStart)) {

                    if (content.contains("/" + tmpStart)) {
                        tmpStart = "";
                        newFiles.add(tmpValue);
                        tmpValue = "";
                        continue;
                    } else {
                        continue;
                    }

                }

                HashBasedTable<String, String, String> template = reNameConfig.getPom().getTemplate();
                Boolean t = false;
                for (Entry<String, Map<String, String>> stringMapEntry : template.rowMap().entrySet()) {
                    if (FileUtil.getParent(file, 1).getName().contains(stringMapEntry.getKey())) {
                        for (Entry<String, String> vv : stringMapEntry.getValue().entrySet()) {
                            if (content.contains(vv.getKey())) {
                                t = true;
                                tmpStart = vv.getKey();
                                tmpValue = vv.getValue();
                                break;
                            }
                        }
                    }
                }

                if (t) {
                    continue;
                }

                Map<String, String> c = reNameConfig.getPom().getReplace();
                for (Entry<String, String> vc : c.entrySet()) {//groupId
                    if (content.contains(vc.getKey())) {
                        content = content.replace(vc.getKey(),
                            vc.getValue());
                        break;
                    }
                }
                newFiles.add(content);

            }
            //	log.info("转换后的文件内容为 {}", newFiles);
            //重新写入文件
            FileUtil.writeLines(newFiles, file, Charset.defaultCharset(), false);
        }
    }

    /**
     * 列出目录文件<br>
     * 给定的绝对路径不能是压缩包中的路径
     *
     * @return 文件列表（包含目录）
     */
    public static File[] ls(File file) {
        if (file == null) {
            return null;
        }

        if (file.isDirectory()) {
            return file.listFiles();
        }
        return null;
    }

    public static void ymlValidate(File file) {
        if (FileUtil.getType(file).equals("yml")) {
            List<String> files = FileUtil.readLines(file, Charset.defaultCharset());
            List<String> newFiles = Lists.newArrayList();
            for (String v : files) {
                String content = v;

                Map<String, String> c = reNameConfig.getYml().getReplace();
                for (Entry<String, String> vc : c.entrySet()) {//groupId
                    if (content.contains(vc.getKey())) {
                        content = content.replace(vc.getKey(),
                            vc.getValue());
                        break;
                    }
                }
                newFiles.add(content);

            }
            //log.info("转换后的文件内容为 {}", newFiles);
            //重新写入文件
            FileUtil.writeLines(newFiles, file, Charset.defaultCharset(), false);
        }
    }

    public static void logbackValidate(File file) {

        String fileName = file.getName();
        if (fileName.contains("logback")) {
            List<String> files = FileUtil.readLines(file, Charset.defaultCharset());
            List<String> newFiles = Lists.newArrayList();
            for (String v : files) {
                String content = v;
                if (content.contains("~")) {
                    continue;
                }
                Map<String, String> c = reNameConfig.getLogBack().getReplace();
                for (Entry<String, String> vc : c.entrySet()) {//groupId
                    if (content.contains(vc.getKey())) {
                        content = content.replace(vc.getKey(),
                            vc.getValue());
                        break;
                    }
                }
                newFiles.add(content);

            }
            //log.info("转换后的文件内容为 {}", newFiles);
            //重新写入文件
            FileUtil.writeLines(newFiles, file, Charset.defaultCharset(), false);
        }
    }

    public static void javaValidate(File file) {
        if (FileUtil.getType(file).equals("java")) {
            List<String> files = FileUtil.readLines(file, Charset.defaultCharset());
            List<String> newFiles = Lists.newArrayList();
            for (String v : files) {
                String content = v;
                if (content.contains("~")) {
                    continue;
                }
                Map<String, String> re = reNameConfig.getJavaSource().getRemove();
                Boolean hasHit = false;
                for (Entry<String, String> vc : re.entrySet()) {//groupId
                    if (content.contains(vc.getKey())) {
                        hasHit = true;
                        break;
                    }
                }
                if (hasHit) {
                    continue;
                }

                Map<String, String> c = reNameConfig.getJavaSource().getReplace();
                for (Entry<String, String> vc : c.entrySet()) {//groupId
                    if (content.contains(vc.getKey())) {
                        content = content.replace(vc.getKey(),
                            vc.getValue());
                        break;
                    }
                }
                newFiles.add(content);

            }
            //log.info("转换后的文件内容为 {}", newFiles);
            //重新写入文件
            FileUtil.writeLines(newFiles, file, Charset.defaultCharset(), false);
        }

    }

    public static void dockerfileValidate(File file) {
        String fileName = file.getName();
        if (fileName.contains("Dockerfile")) {
            List<String> files = FileUtil.readLines(file, Charset.defaultCharset());
            List<String> newFiles = Lists.newArrayList();
            for (String v : files) {
                String content = v;
                if (content.contains("~")) {
                    continue;
                }
                Map<String, String> c = reNameConfig.getDockerfile().getReplace();
                for (Entry<String, String> vc : c.entrySet()) {//groupId
                    if (content.contains(vc.getKey())) {
                        content = content.replace(vc.getKey(),
                            vc.getValue());
                        break;
                    }
                }
                newFiles.add(content);

            }
            //log.info("转换后的文件内容为 {}", newFiles);
            //重新写入文件
            FileUtil.writeLines(newFiles, file, Charset.defaultCharset(), false);

        }
    }

    public static void cssValidate(File file) {
        if (FileUtil.getType(file).equals("css")) {
            List<String> files = FileUtil.readLines(file, Charset.defaultCharset());
            List<String> newFiles = Lists.newArrayList();
            for (String v : files) {
                String content = v;
                if (content.contains("~")) {
                    continue;
                }

                Map<String, String> re = reNameConfig.getCss().getRemove();
                Boolean hasHit = false;

                for (Entry<String, String> vc : re.entrySet()) {//groupId
                    if (content.contains(vc.getKey())) {
                        hasHit = true;
                        break;
                    }
                }
                if (hasHit) {
                    continue;
                }

                Map<String, String> c = reNameConfig.getCss().getReplace();
                for (Entry<String, String> vc : c.entrySet()) {//groupId
                    if (content.contains(vc.getKey())) {
                        content = content.replace(vc.getKey(),
                            vc.getValue());
                        break;
                    }
                }
                newFiles.add(content);

            }
            //log.info("转换后的文件内容为 {}", newFiles);
            //重新写入文件
            FileUtil.writeLines(newFiles, file, Charset.defaultCharset(), false);
        }
    }

    public static void ftlValidate(File file) {

        if (FileUtil.getType(file).equals("ftl")) {
            List<String> files = FileUtil.readLines(file, Charset.defaultCharset());
            List<String> newFiles = Lists.newArrayList();
            for (String v : files) {
                String content = v;
                if (content.contains("~")) {
                    continue;
                }

                Map<String, String> c = reNameConfig.getFtl().getReplace();
                for (Entry<String, String> vc : c.entrySet()) {//groupId
                    if (content.contains(vc.getKey())) {
                        content = content.replace(vc.getKey(),
                            vc.getValue());
                        break;
                    }
                }
                newFiles.add(content);

            }
            //log.info("转换后的文件内容为 {}", newFiles);
            //重新写入文件
            FileUtil.writeLines(newFiles, file, Charset.defaultCharset(), false);
        }
    }

    public static void factoriesValidate(File file) {

        if (FileUtil.getType(file).equals("factories")) {
            List<String> files = FileUtil.readLines(file, Charset.defaultCharset());
            List<String> newFiles = Lists.newArrayList();
            for (String v : files) {
                String content = v;
                if (content.contains("~")) {
                    continue;
                }

                Map<String, String> c = reNameConfig.getFactories().getReplace();
                for (Entry<String, String> vc : c.entrySet()) {//groupId
                    if (content.contains(vc.getKey())) {
                        content = content.replace(vc.getKey(),
                            vc.getValue());
                        break;
                    }
                }
                newFiles.add(content);

            }
            //log.info("转换后的文件内容为 {}", newFiles);
            //重新写入文件
            FileUtil.writeLines(newFiles, file, Charset.defaultCharset(), false);
        }

    }

    public static void txtValidate(File file) {
        if (FileUtil.getType(file).equals("txt")) {
            List<String> files = FileUtil.readLines(file, Charset.defaultCharset());
            List<String> newFiles = Lists.newArrayList();
            for (String v : files) {
                String content = v;
                if (content.contains("~")) {
                    continue;
                }

                Map<String, String> c = reNameConfig.getTxt().getReplace();
                for (Entry<String, String> vc : c.entrySet()) {//groupId
                    if (content.contains(vc.getKey())) {
                        content = content.replace(vc.getKey(),
                            vc.getValue());
                        break;
                    }
                }
                newFiles.add(content);

            }
            //log.info("转换后的文件内容为 {}", newFiles);
            //重新写入文件
            FileUtil.writeLines(newFiles, file, Charset.defaultCharset(), false);
        }

    }

    public static void propertiesValidate(File file) {
        if (FileUtil.getType(file).equals("properties")) {
            List<String> files = FileUtil.readLines(file, Charset.defaultCharset());
            List<String> newFiles = Lists.newArrayList();
            for (String v : files) {
                String content = v;
                if (content.contains("~")) {
                    continue;
                }

                Map<String, String> re = reNameConfig.getProperties().getRemove();
                Boolean hasHit = false;

                for (Entry<String, String> vc : re.entrySet()) {//groupId
                    if (content.contains(vc.getKey())) {
                        hasHit = true;
                        break;
                    }
                }
                if (hasHit) {
                    continue;
                }

                Map<String, String> c = reNameConfig.getProperties().getReplace();
                for (Entry<String, String> vc : c.entrySet()) {//groupId
                    if (content.contains(vc.getKey())) {
                        content = content.replace(vc.getKey(),
                            vc.getValue());
                        break;
                    }
                }
                newFiles.add(content);

            }
            //log.info("转换后的文件内容为 {}", newFiles);
            //重新写入文件
            FileUtil.writeLines(newFiles, file, Charset.defaultCharset(), false);
        }
    }

    public static void mapperValidate(File file) {
        String fileName = file.getName();
        if (!fileName.contains("Mapper")) {
            return;
        }
        if (FileUtil.getType(file).equals("xml")) {
            List<String> files = FileUtil.readLines(file, Charset.defaultCharset());
            List<String> newFiles = Lists.newArrayList();
            for (String v : files) {
                String content = v;
                if (content.contains("~")) {
                    continue;
                }

                Map<String, String> re = reNameConfig.getXml().getRemove();
                Boolean hasHit = false;

                for (Entry<String, String> vc : re.entrySet()) {//groupId
                    if (content.contains(vc.getKey())) {
                        hasHit = true;
                        break;
                    }
                }
                if (hasHit) {
                    continue;
                }

                Map<String, String> c = reNameConfig.getXml().getReplace();
                for (Entry<String, String> vc : c.entrySet()) {//groupId
                    if (content.contains(vc.getKey())) {
                        content = content.replace(vc.getKey(),
                            vc.getValue());
                        break;
                    }
                }
                newFiles.add(content);

            }
            //log.info("转换后的文件内容为 {}", newFiles);
            //重新写入文件
            FileUtil.writeLines(newFiles, file, Charset.defaultCharset(), false);
        }
    }

    public static void mdValidate(File file) {
        if (FileUtil.getType(file).equals("md")) {
            List<String> files = FileUtil.readLines(file, Charset.defaultCharset());
            List<String> newFiles = Lists.newArrayList();
            for (String v : files) {
                String content = v;
                if (content.contains("~")) {
                    continue;
                }

                Map<String, String> c = reNameConfig.getMd().getReplace();
                for (Entry<String, String> vc : c.entrySet()) {//groupId
                    if (content.contains(vc.getKey())) {
                        content = content.replace(vc.getKey(),
                            vc.getValue());
                        break;
                    }
                }
                newFiles.add(content);

            }
            //log.info("转换后的文件内容为 {}", newFiles);
            //重新写入文件
            FileUtil.writeLines(newFiles, file, Charset.defaultCharset(), false);
        }
    }

    public static void licenseValidate(File file) {
        String fileName = file.getName();
        if (!fileName.contains("LICENSE")) {
            return;
        }

        List<String> files = FileUtil.readLines(file, Charset.defaultCharset());
        List<String> newFiles = Lists.newArrayList();
        for (String v : files) {
            String content = v;
            if (content.contains("~")) {
                continue;
            }

            Map<String, String> c = reNameConfig.getLicense().getReplace();
            for (Entry<String, String> vc : c.entrySet()) {//groupId
                if (content.contains(vc.getKey())) {
                    content = content.replace(vc.getKey(),
                        vc.getValue());
                    break;
                }
            }
            newFiles.add(content);

        }
        //log.info("转换后的文件内容为 {}", newFiles);
        //重新写入文件
        FileUtil.writeLines(newFiles, file, Charset.defaultCharset(), false);
    }

    public static void vmValidate(File file) {
        if (FileUtil.getType(file).equals("vm")) {
            List<String> files = FileUtil.readLines(file, Charset.defaultCharset());
            List<String> newFiles = Lists.newArrayList();
            for (String v : files) {
                String content = v;
                if (content.contains("~")) {
                    continue;
                }
                Map<String, String> re = reNameConfig.getVm().getRemove();
                Boolean hasHit = false;

                for (Entry<String, String> vc : re.entrySet()) {//groupId
                    if (content.contains(vc.getKey())) {
                        hasHit = true;
                        break;
                    }
                }
                if (hasHit) {
                    continue;
                }
                Map<String, String> c = reNameConfig.getVm().getReplace();
                for (Entry<String, String> vc : c.entrySet()) {//groupId
                    if (content.contains(vc.getKey())) {
                        content = content.replace(vc.getKey(),
                            vc.getValue());
                        break;
                    }
                }
                newFiles.add(content);

            }
            //log.info("转换后的文件内容为 {}", newFiles);
            //重新写入文件
            FileUtil.writeLines(newFiles, file, Charset.defaultCharset(), false);
        }
    }

    public static void sqlValidate(File file) {
        if (FileUtil.getType(file).equals("sql")) {
            List<String> files = FileUtil.readLines(file, Charset.defaultCharset());
            List<String> newFiles = Lists.newArrayList();
            for (String v : files) {
                String content = v;
                if (content.contains("~")) {
                    continue;
                }

                Map<String, String> c = reNameConfig.getSql().getReplace();
                for (Entry<String, String> vc : c.entrySet()) {//groupId
                    if (content.contains(vc.getKey())) {
                        content = content.replace(vc.getKey(),
                            vc.getValue());
                    }
                }
                newFiles.add(content);

            }
            //log.info("转换后的文件内容为 {}", newFiles);
            //重新写入文件
            FileUtil.writeLines(newFiles, file, Charset.defaultCharset(), false);
        }
    }

    public static void htmlValidate(File file) {
        if (FileUtil.getType(file).equals("html")) {
            List<String> files = FileUtil.readLines(file, Charset.defaultCharset());
            List<String> newFiles = Lists.newArrayList();
            for (String v : files) {
                String content = v;
                if (content.contains("~")) {
                    continue;
                }
                Map<String, String> re = reNameConfig.getHtml().getRemove();
                Boolean hasHit = false;

                for (Entry<String, String> vc : re.entrySet()) {//groupId
                    if (content.contains(vc.getKey())) {
                        hasHit = true;
                        break;
                    }
                }
                if (hasHit) {
                    continue;
                }
                Map<String, String> c = reNameConfig.getHtml().getReplace();
                for (Entry<String, String> vc : c.entrySet()) {//groupId
                    if (content.contains(vc.getKey())) {
                        content = content.replace(vc.getKey(),
                            vc.getValue());
                    }
                }
                newFiles.add(content);

            }
            //log.info("转换后的文件内容为 {}", newFiles);
            //重新写入文件
            FileUtil.writeLines(newFiles, file, Charset.defaultCharset(), false);
        }
    }

    public static void projectFileParse(File file) {

        //在验证pom
        FileUtils.pomValidate(file);

        //在验证yml
        FileUtils.ymlValidate(file);

        //logback-spring
        FileUtils.logbackValidate(file);

        //logback-spring
        FileUtils.dockerfileValidate(file);

        //css
        FileUtils.cssValidate(file);

        //ftl
        FileUtils.ftlValidate(file);

        //ftl
        FileUtils.factoriesValidate(file);

        //md
        FileUtils.mdValidate(file);


        //md
        FileUtils.licenseValidate(file);

        //vm
        FileUtils.vmValidate(file);

        //vm
        FileUtils.htmlValidate(file);
        //txt
        FileUtils.txtValidate(file);

        //txt
        FileUtils.propertiesValidate(file);
        //txt
        FileUtils.mapperValidate(file);
        //验证java

        //logback-spring
        FileUtils.javaValidate(file);

        //先验证文件名
        FileUtils.fileNameValidate(file);

    }
}
